package org.bootlimn.common;
/**
 * @author satya
 * common methods
 * 
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Common {
	/** Program version. */
	public static final String VERSION = "bootlimn 0.1";
	
	/** Process description date format. */
	private static final DateFormat DESC_TIME_FORMAT =
		new SimpleDateFormat("mm:ss.SSS");
	
	public static final Locale LOCALE = Locale.US;
	
	/**
	 * Returns a buffered reader suitable for reading the input stream.
	 *  
	 * @param is            input stream to read
	 * @return              buffered reader
	 * @throws IOException  if an I/O error occurs
	 */
	public static BufferedReader getReader(InputStream is) throws IOException {
		BufferedReader reader =
			new BufferedReader(new InputStreamReader(is, "ISO-8859-1"));
		return reader;
	}
	
	public static String loadFile(File file) throws IOException {
		InputStream is = null;
		String path = file.getPath();
		if (file.exists()) {
			is = new FileInputStream(path);
		} else {
			path = "/" + path.replaceFirst("\\./", "");
			is = Common.class.getResourceAsStream(path);
		}
		if (is == null) {
			throw new FileNotFoundException("File or resource " + path + " not found");
		}
		try {
			byte[] buff = new byte[8096];
			StringBuffer sb = new StringBuffer();
			while (true) {
				int read = is.read(buff);
				if (read < 0) {
					break;
				}
				sb.append(new String(buff, 0, read));
			}
			return sb.toString();
		} finally {
			is.close();
		}
	}
	
	/**
	 * Whether the running JVM supports PNG encoding.  Some runtime
	 * environments (e.g. those based on GNU Classpath) don't have the
	 * necessary Graphics2D support.
	 * 
	 * @return  whether this JVM supports PNG encoding
	 */
	public static boolean isPNGSupported() {
		String vm = System.getProperty("java.vm.info");
		// disable PNG encoding with jamvm and gcj
		if (vm == null || vm.indexOf("gcj") != -1) {
			return false;
		} else {
			return true;
		}
	}
}